﻿using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

[Serializable]
public class AudioList 
{
    public List<AudioClip> BGMClips;
    public AudioClip SelectSound;
    public AudioClip StartSound;
    public AudioClip JumpSound;
    public AudioClip PushSound;
    public AudioClip DieSound;
    public AudioClip HitBrickSound;
    public AudioClip HitMonsterSound;
    public AudioClip CatchSound;
    public AudioClip StoneHitMonsterSound;
    public AudioClip SuccessSound;
    public AudioClip GameOverSound;
}

public enum SoundType 
{
    Select,
    Start,
    Jump,
    Push,
    Die,
    HitBrick,
    HitMonster,
    Catch,
    StoneHitMonster,
    Success,
    GameOver,
}
public class AudioManager : MonoBehaviour
{
    private void Update()
    {
        UodateAudio();
    }

    public static AudioManager Instance;

    private void Awake()
    {
        if (Instance != null && Instance != this)
        {
            Destroy(gameObject);
            return;
        }
        Instance = this;
        //DontDestroyOnLoad(gameObject);
        _BGMSource=GetComponent<AudioSource>();
    }
 
    public AudioList AudioList;

    [SerializeField]private AudioSource _BGMSource;

    public void PlayBGM(int stageIndex,bool isLoop) 
    {
        if (!_BGMSource.isPlaying) 
        {
            AudioClip clip = AudioList.BGMClips[stageIndex - 1];
            _BGMSource.clip = clip;
            _BGMSource.loop = isLoop;
            _BGMSource.Play();
        }
    }
    /// <summary>
    ///播放速率
    /// </summary>
    /// <param name="pitchValue"></param>
    public void SetPitch(float pitchValue) 
    {
        if (_BGMSource.isPlaying) 
        {
            _BGMSource.pitch = pitchValue;
        }
    }

    public void StopBGM(int stageIndex, bool isLoop)
    {
        if (_BGMSource.isPlaying) 
        {
            _BGMSource.Stop();
        }
        
    }




    private List<AudioSource> _soundList = new List<AudioSource>();

    public void PlaySound(SoundType soundType) 
    {
        AudioClip clip = null;
        switch (soundType)
        {
            case SoundType.Select:
                clip = AudioList.SelectSound;
                break;
            case SoundType.Start:
                clip= AudioList.StartSound;
                break;
            case SoundType.Jump:
                clip= AudioList.JumpSound;
                break;
            case SoundType.Push:
                clip= AudioList.PushSound;
                break;
            case SoundType.Die:
                clip= AudioList.DieSound;
                break;
            case SoundType.HitBrick:
                clip= AudioList.HitBrickSound;
                break;
            case SoundType.HitMonster:
                clip= AudioList.HitMonsterSound;
                break;
            case SoundType.Catch:
                clip= AudioList.CatchSound;
                break;
            case SoundType.StoneHitMonster:
                clip = AudioList.StoneHitMonsterSound;
                break;
            case SoundType.Success:
                clip= AudioList.SuccessSound;
                break;
            case SoundType.GameOver:
                clip= AudioList.GameOverSound;
                break;
            default:
                break;
        }

        AudioSource source=this.gameObject.AddComponent<AudioSource>();
        source.playOnAwake = false;
        source.clip=clip;
        source.loop = false;
        source.Play();
        _soundList.Add(source);
    }
    private void UodateAudio()
    {
        for (int i = _soundList.Count - 1; i >= 0; i--)
        {
            if (!_soundList[i].isPlaying)
            {
                GameObject.Destroy(_soundList[i]);
                _soundList.RemoveAt(i);
            }
        }
    }
}
